package cn.tedu.overhaul.management.controller;

import cn.tedu.overhaul.management.dto.persist.mapper.OverhaulRentMapper;
import cn.tedu.overhaul.management.pojo.dto.OverhaulRentAddNewDTO;
import cn.tedu.overhaul.management.pojo.dto.OverhaulRentUpdateDTO;
import cn.tedu.overhaul.management.pojo.vo.RentUserVO;
import cn.tedu.overhaul.management.pojo.vo.RentVO;
import cn.tedu.overhaul.management.service.IOverhaulRentService;
import cn.tedu.rtms.commons.pojo.vo.PageData;
import cn.tedu.rtms.commons.web.JsonResult;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/overhaul/Rent")
@Api(tags = "02. 借用记录模块")
@Validated
public class OverhaulRentController {

    @Autowired
    IOverhaulRentService iOverhaulRentService;
    @Autowired
    OverhaulRentMapper overhaulRentMapper;

    @PostMapping("/add-new")
    @ApiOperation("添加借用信息")
    @ApiOperationSupport(order = 100)
    public JsonResult<Void> addNew(@ModelAttribute @Valid OverhaulRentAddNewDTO overhaulRentAddNewDTO) {
        log.debug("开始处理[添加借用工具信息] 参数:{}", overhaulRentAddNewDTO);
        iOverhaulRentService.addNew(overhaulRentAddNewDTO);
        return JsonResult.ok();
    }

    @GetMapping("")
    @ApiOperation("查询借用记录")
    @ApiOperationSupport(order = 300)
    public JsonResult<PageData<RentVO>> selectRent(@Range(min=1,message="请提交有效的页码值!")Integer page, String queryType){
        log.debug("开始处理【查询检修记录表】的请求，页码：{}", page);
        Integer pageNum = page == null ? 1 : page;
        PageData<RentVO> pageData;
        if ("all".equals(queryType)) {
            pageData =iOverhaulRentService.selectRent(pageNum, Integer.MAX_VALUE);
            log.debug("*************************************pageData1：{}", pageData);
        }else{
            pageData =iOverhaulRentService.selectRent(pageNum);
            log.debug("**************************************pageData2：{}", pageData);
        }
        return JsonResult.ok(pageData);
    }

    @PostMapping("/{id:[0-9]+}")
    @ApiOperation("归还工具")
    @ApiOperationSupport(order = 310)
    public JsonResult<Void> getStandardById(@PathVariable Long id ,@RequestParam("toolNumber") String toolNumber){
        log.debug("开始处理【根据ID归还工具】的请求，参数：{}", id);
        iOverhaulRentService.updateEndTime(id,toolNumber);
        return JsonResult.ok();
    }

    @PostMapping("/{id:[0-9]+}/delete")
    @ApiOperation("根据ID删除借用记录")
    @PreAuthorize("hasAuthority('/overhaul/Rent/delete')")
    public JsonResult<Void> delete(@PathVariable @Range(min = 1, message = "请提交有效的ID值！") Long id) {
        log.debug("开始处理【根据ID删除借用记录】的请求，参数：{}", id);
        iOverhaulRentService.delete(id);
        return JsonResult.ok();
    }

    @GetMapping("/All/{id:[0-9]+}")
    @ApiOperation("查询借用记录")
    @ApiOperationSupport(order = 320)
    public JsonResult<RentVO> getStandardByIdAll(@PathVariable @Range(min = 1, message = "请提交有效的ID值！") Long id){
        log.debug("开始处理【根据ID修改面板查询借用记录】的请求，参数：{}", id);
        RentVO queryResult=iOverhaulRentService.getStandardByIdAll(id);
        return JsonResult.ok(queryResult);
    }

    @GetMapping("/loadAll")
    @ApiOperation("查询用户名")
    @ApiOperationSupport(order = 320)
    public JsonResult<List<RentUserVO>> getLoadAll(){
        log.debug("开始处理查询用户名的请求");
        List<RentUserVO> queryResult=overhaulRentMapper.getLoadAll();
        log.debug("得到查询的用户名{}",queryResult.toString());
        return JsonResult.ok(queryResult);
    }


    @PostMapping("/{id:[0-9]+}/update")
    @ApiOperation("修改借用记录")
    @ApiOperationSupport(order = 330)
    @PreAuthorize("hasAuthority('/overhaul/Rent/update')")
    public JsonResult<Void> updateInfoById(@PathVariable @Range(min = 1, message = "请提交有效的ID值！") Long id ,@Valid OverhaulRentUpdateDTO overhaulRentUpdateDTO){
        log.debug("开始处理【修改借用记录】的请求，ID：{}，新数据：{}", id, overhaulRentUpdateDTO);
        iOverhaulRentService.updateInfoById(id,overhaulRentUpdateDTO);
        return JsonResult.ok();
    }
}
